package mx.vlabs.soapdroid.wsdl2java;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.ow2.easywsdl.schema.api.SimpleType;
import org.ow2.easywsdl.schema.api.Type;

import mx.vlabs.soapdroid.utils.Utils;

public class SDroidUtils {
	
	public static final List<String> FORBIDDEN_FIELD_NAMES = new ArrayList<String>() {{
		
		add("name");
		add("return");
		add("namespace");
		add("double");
		add("int");
		add("float");
		add("byte");
		add("long");
		add("String");
		add("char");
		
	}};
	
	public static final List<String> NATIVE_TYPES = new ArrayList<String>() {{
		
		add("double");
		add("int");
		add("float");
		add("byte");
		add("long");
		add("String");
		add("char");
		add("base64Binary");
		
	}};
	
	public static final String EMPTY_QUOTES = "\"\"";
	
	public static String buildJavaName(String name){
		
		name = Utils.uncapitalizeFirstLetter(name);
		
		if(FORBIDDEN_FIELD_NAMES.contains(name)){
			name = "_"+name;
		}
		
		return name;
	}
	
	public static String buildJavaPackageFromNamespace(String namespace){
		
		URL url = null;
		try {
			url = new URL(namespace);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			throw new IllegalArgumentException(e);
		}

		
		String[] parts = url.getHost()
							.toLowerCase()
							.replace("www.", "")
							.split("\\.");
		
		StringBuilder sb = new StringBuilder();
		
		for (int i = parts.length-1; i >= 0; i--) {
			
			if(i!=parts.length-1)
			sb.append(".");
			
			sb.append(parts[i]);
		}
		

		return sb.toString();
		
	}
	
	public static boolean isEnum(SimpleType type){
		return !NATIVE_TYPES.contains(type.getQName().getLocalPart());
		
	}
}
